using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.IO.Ports;using System.Windows.Threading;using Microsoft.Win32;using System.IO;namespace 串口助手{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public SerialPortModel Port { get; set; } public bool IsRecSend1 { get; set; } public bool IsRecSend2 { get; set; } public bool IsStop { get; set; } private DispatcherTimer sendTimer1 = new DispatcherTimer(); private DispatcherTimer sendTimer2 = new DispatcherTimer(); public MainWindow() { InitializeComponent(); Port = new SerialPortModel(); Port.DataReceived = new SerialDataReceivedEventHandler(Port_DataReceived); string[] DelayTimes = { "0", "500", "1000", "1500", "2000", "3000", "4000", "5000", "6000", "7000", "8000", "9000", "10000"}; cmbSend1.ItemsSource = DelayTimes; cmbSend2.ItemsSource = DelayTimes; this.DataContext = this; cmbPortName.SelectedIndex = Properties.Settings.Default.PortName; cmbBaudRate.SelectedIndex = Properties.Settings.Default.BaudRate; txtSend1.Text = Properties.Settings.Default.Send1; txtSend2.Text = Properties.Settings.Default.Send2; btnOpenPort.Click = delegate(object sender, RoutedEventArgs e) { if ((bool)btnOpenPort.IsChecked) { Port.PortName = (string)cmbPortName.SelectedItem; Port.BaudRate = Convert.ToInt32(cmbBaudRate.SelectedItem); try { Port.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); btnOpenPort.IsChecked = false; return; } cmbBaudRate.IsEnabled = false; cmbDataBit.IsEnabled = false; cmbPortName.IsEnabled = false; cmbParity.IsEnabled = false; cmbStopBit.IsEnabled = false; } else { Port.Close(); cmbBaudRate.IsEnabled = true; cmbDataBit.IsEnabled = true; cmbPortName.IsEnabled = true; cmbParity.IsEnabled = true; cmbStopBit.IsEnabled = true; } }; btnClearText.Click = delegate(object sender, RoutedEventArgs e) { //txtRecData.Clear(); txtRecData.Document.Blocks.Clear(); }; btnClearZero.Click = delegate(object sender, RoutedEventArgs e) { Port.ClearCounter(); }; btnSave.Click = delegate(object sender, RoutedEventArgs e) { SaveFileDialog sfd = new SaveFileDialog() { Filter = "文本|*.txt" }; if (sfd.ShowDialog() == true) { //File.WriteAllText(sfd.FileName, txtRecData.Text); //using (FileStream stream = File.OpenWrite(sfd.FileName)) //{ // TextRange documentTextRange = new TextRange(txtRecData.Document.ContentStart, txtRecData.Document.ContentEnd); // string dataFormat = DataFormats.Text; // string ext = System.IO.Path.GetExtension(sfd.FileName); // if (String.Compare(ext, ".xaml", true) == 0) // { // dataFormat = DataFormats.Xaml; // } // else if (String.Compare(ext, ".rtf", true) == 0) // { // dataFormat = DataFormats.Rtf; // } // documentTextRange.Save(stream, dataFormat); //} TextRange textRange = new TextRange(txtRecData.Document.ContentStart, txtRecData.Document.ContentEnd); File.WriteAllText(sfd.FileName, textRange.Text); } }; btnSend1.Click = delegate(object sender, RoutedEventArgs e) { SendData(txtSend1.Text); }; cmbSend1.SelectionChanged = delegate(object sender, SelectionChangedEventArgs e) { int time = Convert.ToInt32(cmbSend1.SelectedValue); if (time > 0) { sendTimer1.Interval = new TimeSpan(time * 10000); sendTimer1.Start(); } else { sendTimer1.Stop(); } }; sendTimer1.Tick = delegate(object sender, EventArgs e) { SendData(txtSend1.Text); }; btnSend2.Click = delegate(object sender, RoutedEventArgs e) { SendData(txtSend2.Text); }; cmbSend2.SelectionChanged = delegate(object sender, SelectionChangedEventArgs e) { int time = Convert.ToInt32(cmbSend2.SelectedValue); if (time > 0) { sendTimer2.Interval = new TimeSpan(time * 10000); sendTimer2.Start(); } else { sendTimer2.Stop(); } }; sendTimer2.Tick = delegate(object sender, EventArgs e) { SendData(txtSend2.Text); }; txtRecData.TextChanged = delegate(object sender, TextChangedEventArgs e) { txtRecData.ScrollToEnd(); }; this.Closing = delegate(object sender, System.ComponentModel.CancelEventArgs e) { Properties.Settings.Default.PortName = cmbPortName.SelectedIndex; Properties.Settings.Default.BaudRate = cmbBaudRate.SelectedIndex; Properties.Settings.Default.Send1 = txtSend1.Text; Properties.Settings.Default.Send2 = txtSend2.Text; Properties.Settings.Default.Save(); }; this.Activated = delegate(object sender, EventArgs e) { string str = Clipboard.GetText(); Clipboard.SetText(str.Replace("\r\n", "")); }; } private void SendData(string str) { if (IsStop || !Port.IsShowSend) { Port.SendDataUpdate(str); } else { //txtRecData.AppendText(Port.SendDataUpdate(str) Environment.NewLine); //Paragraph p = new Paragraph(); //p.Foreground = Brushes.Blue; //Run r = new Run(Port.SendDataUpdate(str)); //r.Foreground = Brushes.Blue; //p.Inlines.Add(r); //txtRecData.Document.Blocks.Add(p); Paragraph p = (Paragraph)txtRecData.Document.Blocks.FirstBlock; Run r = new Run(Port.SendDataUpdate(str) Environment.NewLine); r.Foreground = Brushes.DarkBlue; p.Inlines.Add(r); } } public delegate void UpdateBytesDelegate(byte[] data); void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) { int length = Port.BytesToRead; byte[] data = new byte[length]; for (int i = 0; i < length; i ) { data[i] = (byte)Port.ReadByte(); } Dispatcher.Invoke(new UpdateBytesDelegate(UpdateBytesbox), data); } private void UpdateBytesbox(byte[] data) { if (IsStop) { Port.ReceiveDataUpdate(data); } else { //txtRecData.AppendText(Port.ReceiveDataUpdate(data) Environment.NewLine); Paragraph p = (Paragraph)txtRecData.Document.Blocks.FirstBlock; Run r = new Run(Port.ReceiveDataUpdate(data) Environment.NewLine); r.Foreground = Brushes.DarkRed; p.Inlines.Add(r); } if (IsRecSend1) { SendData(txtSend1.Text); } else if (IsRecSend2) { SendData(txtSend2.Text); } } }}
下载C# 串口调试助手 工具源码(wpf)用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论